<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/16
 * Time: 15:53
 */

namespace app\admin\model;


class CardRule extends DataBase
{
    protected $name = 'card_rule';
    protected $autoWriteTimestamp = true;

    public function getTypeAttr($value)
    {
        $status = [1=>'座驾',2=>'普通礼物'];
        return $status[$value];
    }
    public function getDataList()
    {
        $page_config = array(
            'page' => input('get.offset',1),
            'list_rows' => input('get.limit',10)
        );
        $order = input('sort','a.id').' '.input('order','asc');
        $where = [];
        $key = input('get.key'); $value = input('get.value');
        if($key=='type' && $value=='座驾'){
            $where[] = ['a.'.$key,'=',1];
        }elseif($key=='type' && $value=='普通礼物'){
            $where[] = ['a.'.$key,'=',2];
        }elseif($key=='id' && !empty($value)){
            $where[] = ['a.'.$key,'=',$value];
        }
        $data = $this
            ->alias('a')
            ->field('a.id,a.type,a.type_id,a.card_num,a.status,b.name')
            ->join('card b','a.card_id = b.id','left')
            ->where($where)
            ->order($order)
            ->paginate($page_config);
        foreach($data as $key => $value){
            if($value['type'] == '普通礼物')
                $data[$key]['gift_name'] = Gift::where('id='.$value['type_id'])->value('gift_name');
            if($value['type'] == '座驾')
                $data[$key]['gift_name'] = Vehicle::where('vehicle_id='.$value['type_id'])->value('vehicle_name');
        }
        return $data;
    }
}